package com.ea.eadp.nexus.test.pageDefinition.lockbox;

import com.ea.eadp.nexus.test.common.PageOperation;
import com.ea.eadp.nexus.test.common.TestUtil;
import com.ea.eadp.nexus.test.pageDefinition.BasePage;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: yjtao
 * Date: 3/23/15
 * Time: 5:04 PM
 * To change this template use File | Settings | File Templates.
 */
public class SignupPage extends BasePage {
    public static final String uri = baseUri + TestUtil.SignupPcConnectURI();
    public static final String redirectURI = "localhost";


    public SignupPage(WebDriver driver) {
        super(driver, SignupPage.class);
    }

    public void navigate(String uri) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(uri);
        driver.get(uri);
    }

    public void setDobMonth(String month) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(month);
        String cssSelector = "#clientreg_dobmonth";
        PageOperation.SelectCssDropDownItem(driver, cssSelector, month);
    }

    public void setDobDay(String day) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(day);
        String cssSelector = "#clientreg_dobday";
        PageOperation.SelectCssDropDownItem(driver, cssSelector, day);
    }

    public void setDobYear(String year) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(year);
        String cssSelector = "#clientreg_dobyear";
        PageOperation.SelectCssDropDownItem(driver, cssSelector, year);
    }

    public void setRegisterCountry(String country) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(country);
        String cssSelector = "#clientreg_country";
        PageOperation.SelectCssDropDownItem(driver, cssSelector, country);
    }

    public void setOriginID(String originID) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(originID);
        PageOperation.SendKeyToElement(driver, "originId", originID);
    }

    /**
     * get attribute 'MaxLength' for Origin ID, confirm if it is '16'
     */
    public String getMaxLengthForOriginID() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.id("originId")).getAttribute("maxlength");
    }

    public void setSignupEmail(String email) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(email);
        PageOperation.SendKeyToElement(driver, "email", email);
    }

    public void setParentEmail(String email) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(email);
        PageOperation.SendKeyToElement(driver, "parentEmail", email);
    }

    public void setSignupPassword(String password) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(password);
        PageOperation.SendKeyToElement(driver, "password", password);
    }

    public void setConfirmPassword(String confirmPassword) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(confirmPassword);
        PageOperation.SendKeyToElement(driver, "confirmPassword", confirmPassword);
    }

    public void setCaptcha(String value) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(value);

        PageOperation.SendKeyToElement(driver, "recaptcha_response_field", value);
    }

    public void checkboxContactMe(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCheckbox(driver, "contactMe", "label-contactMe", check);

    }

    public void checkboxShareInfo(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        PageOperation.SelectCheckbox(driver, "shareInfo", "label-shareInfo", check);

    }

    public void checkboxReadAccept(boolean check) {
        driver.switchTo().window(this.operationWindowHandle);
        if (check && !driver.findElement(By.id("readAccept")).isSelected()) {
            driver.findElement(By.xpath("//li[@id='read-accept-container']/div/div/span")).click();
        } else if (!check && driver.findElement(By.id("readAccept")).isSelected()) {
            driver.findElement(By.xpath("//li[@id='read-accept-container']/div/div/span")).click();
        }
    }

    /**
     * click 'Next' button on s1 page, then redirect to 's2' page
     */
    public void clickToSignupComplete() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath(
                "//*[@id='panel-security']/div/div[2]/a[1]/span/span")).click();

        Thread.sleep(2000);
        if (!TestUtil.getCurrentUrl(driver).contains(redirectURI)) {
            throw new Exception("validate goes to redirect uri.");
        }
    }

    /**
     * get the error messages for the element by xpath
     */
    public String getErrorMessageByXPath(String xpath) {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath(xpath)).getText();
    }

    /**
     * the control will become 'field-error' when it has error, get it by xpath
     */
    public String getErrorClassByXPath(String xpath) {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath(xpath)).getAttribute("class").toString();
    }

    /**
     * send tab to move the cursor
     */
    public void sendTab(String elementID) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info("[Tab]");
        driver.findElement(By.id(elementID)).sendKeys("\t");
        Thread.sleep(2000);
    }

    public String getSelectedMonth() {
        return getElementStringByXPath("//div[@id='clientreg_dobmonth']/div[1]/span");
    }

    public String getSelectedDay() {
        return getElementStringByXPath("//div[@id='clientreg_dobday']/div[1]/span");
    }

    public String getSelectedYear() {
        return getElementStringByXPath("//div[@id='clientreg_dobyear']/div[1]/span");
    }

    public String getSelectedCountry() {
        return getElementStringByXPath("//div[@id='clientreg_country']/div[1]/span");
    }

    public String getOriginIDLabel() {
        return getElementStringByXPath("//li[@id='origin-id-container']/div/label");
    }

    public String getOriginIDTip() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        //driver.findElement(By.xpath("//li[@id='origin-id-container']/div/label/span/a")).click();
        driver.findElement(By.cssSelector("a.origin-ux-tooltip-icon")).click();
        return getElementStringByXPath("//li[@id='origin-id-container']/div/label/span/span");
    }

    public String getEmailLabel() {
        return getElementStringByXPath("//li[@id='email-container']/div/label");
    }

    public String getPasswordLabel() {
        return getElementStringByXPath("//li[@id='password-container']/div/label");
    }

    public String getConfirmPwdLabel() {
        return getElementStringByXPath("//li[@id='confirm-password-container']/div/label");
    }

    public String getPrivacyLabel() {
        return getElementStringByXPath("//div[@id='panel-privacy-settings']/h2");
    }

    public String getProfileTip() {
        return getElementStringByXPath("//div[@id='panel-privacy-settings']/p");
    }

    public String getCaptchaLabel() {
        return getElementStringByXPath("//li[@id='captcha-container']/div/label");
    }

    public String getContactMeLabel() {
        return getElementStringByXPath("//label[@id='label-contactMe']");
    }

    public String getShareInfoLabel() {
        return getElementStringByXPath("//label[@id='label-shareInfo']");
    }

    public String getReadAcceptLabel() {
        return getElementStringByXPath("//label[@id='label-readAccept']");
    }

    public String getCreateAccountBtnString() {
        return getElementStringByXPath("//*[@id=\"panel-security\"]/div/div[2]/a[1]/span/span");
    }

    public String getErrorByElementID(String elementID) {
        String actualError = getErrorClassByXPath("//li[@id='" + elementID + "']/div");
        if (actualError.contains("field-error")) {
            String actualErrorMessage =
                    getErrorMessageByXPath("//li[@id='" + elementID + "']/div[1]/span[3]");
            return actualErrorMessage;
        }
        return null;
    }

    public String getErrorForOriginID() {
        return getErrorByElementID("origin-id-container");
    }

    public String getErrorForEmail() {
        return getErrorByElementID("email-container");
    }

    public String getErrorForPassword() {
        return getErrorByElementID("password-container");
    }

    public String getErrorForConfirmPassword() {
        return getErrorByElementID("confirm-password-container");
    }

    public String getMaxLength(String elementID) {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.id(elementID)).getAttribute("maxlength");
    }

    public String getContactUsTooltipString(){
        org.openqa.selenium.WebElement webElement = driver.findElement(By.xpath("//span[@id='contact_us']")) ;
        return webElement.getAttribute("title");
    }
    public String getAccountInfoTooltipString(){
        org.openqa.selenium.WebElement webElement = driver.findElement(By.xpath("//span[@id='account_info']")) ;
        return webElement.getAttribute("title");
    }
    public String getDetailsTooltipString(){
        org.openqa.selenium.WebElement webElement = driver.findElement(By.xpath("//span[@id='details']")) ;
        return webElement.getAttribute("title");
    }
    public String getOriginUxTooltipString(){
        org.openqa.selenium.WebElement webElement = driver.findElement(By.xpath("//li[@id='share-info-container']/div[1]/div/div/div/div[2]/span")) ;
        return webElement.getAttribute("innerHTML");
    }
}
